package com.nocturne.AndroidClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class MainActivity extends Activity {

    private Button btnLED1On;
    private Button btnLED2On;
    private Button btnDance;

    private TextView textViewBtn;

    private boolean isPushBtn1 = false;
    private boolean isPushBtn2 = false;
    private boolean isPushBtnDance = false;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textViewBtn = (TextView) findViewById(R.id.textViewBtn);

        btnLED1On = (Button)findViewById(R.id.btnLED1On);
        btnLED1On.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if(!isPushBtn1) {
                    doCommand("onLED1");
                    isPushBtn1 = true;

                    textViewBtn.setText("LED1 is On");
                } else {
                    doCommand("offLED1");
                    isPushBtn1 = false;

                    textViewBtn.setText("LED1 is Off");
                }
            }
        });


        btnLED2On = (Button)findViewById(R.id.btnLED2On);
        btnLED2On.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if(!isPushBtn2) {
                    doCommand("onLED2");
                    isPushBtn2 = true;

                    textViewBtn.setText("LED2 is On");
                } else {
                    doCommand("offLED2");
                    isPushBtn2 = false;

                    textViewBtn.setText("LED2 is Off");
                }

            }
        });


        btnDance = (Button)findViewById(R.id.btnDance);
        btnDance.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                if(!isPushBtnDance) {
                    doCommand("onDance");
                    isPushBtnDance = true;

                    textViewBtn.setText("Dancing show");
                } else {
                    doCommand("offDance");
                    isPushBtnDance = false;

                    textViewBtn.setText("Dancing off");
                }
            }
        });


    }


    private void doCommand(String command) {

        addSockets(command, "1");
    }

    private String addSockets(String command, String param) {

        String result = null;
        String[] json = {command, param};

        try {
            //Socket s = new Socket("192.168.1.101", 1234);
            Socket s = new Socket("192.168.1.106", 1234);

            ObjectOutputStream out;
            ObjectInputStream in;

            out = new ObjectOutputStream(s.getOutputStream());
            in = new ObjectInputStream(s.getInputStream());

            out.writeObject(json);
            out.flush();

            // response server
            //result = (String) in.readObject();
            //System.out.println(result);


            in.close();
            out.close();

            return result;


        } catch (IOException e) {
            System.out.println(e);
        }

        return null;
    }


}
